package br.com.j7.jdesk.ui.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import br.com.j7.jdesk.library.exception.RepositoryException;
import br.com.j7.jdesk.persistence.repository.CustomerRepository;
import br.com.j7.jdesk.persistence.repository.impl.CustomerRepositoryImpl;
import br.com.j7.jdesk.ui.bean.WebContext;

public class CustomerConverter extends WebContext implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		if(value != null && !"".equals(value)){
			CustomerRepository repository = new CustomerRepositoryImpl();
			try {
				return repository.findByCustomerAndEnterprise(value, getSessionEnterprise());
			} catch (RepositoryException e) {
				throw new RuntimeException(e);
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
		if(value != null)
			return value.toString();
		return null;
	}

}
